package com.hsjk.szwj.module.smedia.service.mediaManager;

import cn.hutool.core.util.ObjectUtil;
import com.aliyun.ice20201109.Client;
import com.aliyun.ice20201109.models.BatchGetMediaInfosRequest;
import com.aliyun.ice20201109.models.BatchGetMediaInfosResponse;
import com.aliyun.ice20201109.models.GetMediaInfoRequest;
import com.aliyun.ice20201109.models.GetMediaInfoResponse;
import com.aliyun.ice20201109.models.ListAllPublicMediaTagsRequest;
import com.aliyun.ice20201109.models.ListAllPublicMediaTagsResponse;
import com.aliyun.ice20201109.models.ListMediaBasicInfosRequest;
import com.aliyun.ice20201109.models.ListMediaBasicInfosResponse;
import com.aliyun.ice20201109.models.ListPublicMediaBasicInfosRequest;
import com.aliyun.ice20201109.models.ListPublicMediaBasicInfosResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.hsjk.szwj.framework.common.exception.ServiceException;
import com.hsjk.szwj.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.hsjk.szwj.module.smedia.controller.admin.mediaManager.vo.GetMediaInfosReqVO;
import com.hsjk.szwj.module.smedia.controller.admin.mediaManager.vo.GetMediaInfosRespVO;
import com.hsjk.szwj.module.smedia.controller.admin.mediaManager.vo.MediaBasicInfosReqVO;
import com.hsjk.szwj.module.smedia.controller.admin.mediaManager.vo.MediaBasicInfosRespVO;
import com.hsjk.szwj.module.smedia.controller.admin.mediaManager.vo.MediaInfoReqVO;
import com.hsjk.szwj.module.smedia.controller.admin.mediaManager.vo.MediaInfoRespVO;
import com.hsjk.szwj.module.smedia.controller.admin.mediaManager.vo.PublicMediaBasicInfosReqVO;
import com.hsjk.szwj.module.smedia.controller.admin.mediaManager.vo.PublicMediaBasicInfosRespVO;
import com.hsjk.szwj.module.smedia.controller.admin.mediaManager.vo.PublicMediaTagsListReqVO;
import com.hsjk.szwj.module.smedia.controller.admin.mediaManager.vo.PublicMediaTagsListRespVO;
import com.hsjk.szwj.module.smedia.convert.mediaManager.MediaManagerConvert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

/**
 * @desc 媒资库相关接口实现类
 * @Date 2023/07/04 14:12
 **/
@Service
@Validated
@Slf4j
public class MediaLibraryImpl implements IMediaLibrary {
    @Value("${ali.media.access-key-id}")
    private String accessKeyId;

    @Value("${ali.media.access-key-secret}")
    private String accessKeySecret;

    @Value("${ali.media.endpoint}")
    private String endpoint;

    @Override
    public MediaInfoRespVO mediaInfo(MediaInfoReqVO reqVO) {
        if (ObjectUtil.isNull(reqVO)) {
            throw new ServiceException(GlobalErrorCodeConstants.BAD_REQUEST.getCode()
                    , "参数不能为空");
        }
        if (ObjectUtil.isNull(reqVO.getMediaId()) && ObjectUtil.isNull(reqVO.getInputUrl())) {
            throw new ServiceException(GlobalErrorCodeConstants.BAD_REQUEST.getCode()
                    , "mediaId，inputUrl必须一个不为空");
        }
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setEndpoint(endpoint);
        MediaInfoRespVO response = null;
        try {
            Client client = new Client(config);
            RuntimeOptions runtime = new RuntimeOptions();
            GetMediaInfoRequest request = new GetMediaInfoRequest();
            request.setMediaId(reqVO.getMediaId());
            request.setInputURL(reqVO.getInputUrl());
            request.setOutputType(reqVO.getOutputType());
            GetMediaInfoResponse mediaInfoWithOptions = client.getMediaInfoWithOptions(request, runtime);
            if (ObjectUtil.isNotNull(mediaInfoWithOptions)) {
                response = MediaManagerConvert.INSTANCE.convertMediaInfoRespVO(mediaInfoWithOptions.getBody());
            }
        } catch (TeaException teaException) {
            log.error(teaException.message);
        } catch (Exception exception) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), exception.getMessage());
        }
        return response;
    }

    @Override
    public MediaBasicInfosRespVO listMediaBasicInfos(MediaBasicInfosReqVO reqVO) {
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setEndpoint(endpoint);
        MediaBasicInfosRespVO response = null;
        try {
            Client client = new Client(config);
            RuntimeOptions runtime = new RuntimeOptions();
            ListMediaBasicInfosRequest request = new ListMediaBasicInfosRequest();
            if (ObjectUtil.isNotEmpty(reqVO.getMediaType())) {
                request.setMediaType(reqVO.getMediaType());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getEndTime())) {
                request.setEndTime(reqVO.getEndTime());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getMediaId())) {
                request.setMediaId(reqVO.getMediaId());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getSource())) {
                request.setSource(reqVO.getSource());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getSortBy())) {
                request.setSortBy(reqVO.getSortBy());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getStartTime())) {
                request.setStartTime(reqVO.getStartTime());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getStatus())) {
                request.setStatus(reqVO.getStatus());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getBusinessType())) {
                request.setBusinessType(reqVO.getBusinessType());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getIncludeFileBasicInfo())) {
                request.setIncludeFileBasicInfo(reqVO.getIncludeFileBasicInfo());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getMaxResults())) {
                request.setMaxResults(reqVO.getMaxResults());
            }
            if (ObjectUtil.isNotEmpty(reqVO.getNextToken())) {
                request.setNextToken(reqVO.getNextToken());
            }
            ListMediaBasicInfosResponse listMediaBasicInfosResponse = client.listMediaBasicInfosWithOptions(request, runtime);
            if (ObjectUtil.isNotNull(listMediaBasicInfosResponse)) {
                response = MediaManagerConvert.INSTANCE.convertMediaBasicInfosRespVO(listMediaBasicInfosResponse.getBody());
            }
        } catch (TeaException teaException) {
            log.error(teaException.message);
        } catch (Exception exception) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), exception.getMessage());
        }
        return response;
    }

    @Override
    public PublicMediaTagsListRespVO listAllPublicMediaTags(PublicMediaTagsListReqVO reqVO) {
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setEndpoint(endpoint);
        PublicMediaTagsListRespVO response = null;
        try {
            Client client = new Client(config);
            RuntimeOptions runtime = new RuntimeOptions();
            ListAllPublicMediaTagsRequest request = new ListAllPublicMediaTagsRequest();
            request.setBusinessType(reqVO.getBusinessType());
            request.setEntityId(reqVO.getEntityId());
            ListAllPublicMediaTagsResponse listAllPublicMediaTagsResponse = client.listAllPublicMediaTagsWithOptions(request, runtime);
            if (ObjectUtil.isNotNull(listAllPublicMediaTagsResponse)) {
                response = MediaManagerConvert.INSTANCE.convertPublicMediaTagsListRespVO(listAllPublicMediaTagsResponse.getBody());
            }
        } catch (TeaException teaException) {
            log.error(teaException.message);
        } catch (Exception exception) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), exception.getMessage());
        }
        return response;
    }

    @Override
    public PublicMediaBasicInfosRespVO listPublicMediaBasicInfos(PublicMediaBasicInfosReqVO reqVO) {
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setEndpoint(endpoint);
        PublicMediaBasicInfosRespVO response = null;
        try {
            Client client = new Client(config);
            RuntimeOptions runtime = new RuntimeOptions();
            ListPublicMediaBasicInfosRequest request = new ListPublicMediaBasicInfosRequest();
            if (ObjectUtil.isNotNull(reqVO)) {
                if (ObjectUtil.isNotEmpty(reqVO.getIncludeFileBasicInfo())) {
                    request.setIncludeFileBasicInfo(reqVO.getIncludeFileBasicInfo());
                }
                if (ObjectUtil.isNotEmpty(reqVO.getMaxResults())) {
                    request.setMaxResults(reqVO.getMaxResults());
                }
                if (ObjectUtil.isNotEmpty(reqVO.getMediaTagId())) {
                    request.setMediaTagId(reqVO.getMediaTagId());
                }
                if (ObjectUtil.isNotEmpty(reqVO.getNextToken())) {
                    request.setNextToken(reqVO.getNextToken());
                }
                if (ObjectUtil.isNotEmpty(reqVO.getPageNo())) {
                    request.setPageNo(reqVO.getPageNo());
                }
                if (ObjectUtil.isNotEmpty(reqVO.getPageSize())) {
                    request.setPageSize(reqVO.getPageSize());
                }
            }
            ListPublicMediaBasicInfosResponse listResponse = client.listPublicMediaBasicInfosWithOptions(request, runtime);
            if (ObjectUtil.isNotNull(listResponse)) {
                response = MediaManagerConvert.INSTANCE.convertPublicMediaBasicInfosRespVO(listResponse.getBody());
            }
        } catch (TeaException error) {
            log.error(error.message);
        } catch (Exception e) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), e.getMessage());
        }
        return response;
    }

    @Override
    public GetMediaInfosRespVO batchGetMediaInfos(GetMediaInfosReqVO reqVO) {
        Config config = new Config();
        config.setAccessKeyId(accessKeyId);
        config.setAccessKeySecret(accessKeySecret);
        config.setEndpoint(endpoint);
        GetMediaInfosRespVO response = null;
        try {
            Client client = new Client(config);
            RuntimeOptions runtime = new RuntimeOptions();
            BatchGetMediaInfosRequest request = new BatchGetMediaInfosRequest();
            if (ObjectUtil.isNotNull(reqVO)) {
                if (ObjectUtil.isNotEmpty(reqVO.getMediaIds())) {
                    request.setMediaIds(reqVO.getMediaIds());
                }
                if (ObjectUtil.isNotEmpty(reqVO.getAdditionType())) {
                    request.setAdditionType(reqVO.getAdditionType());
                }
            }
            BatchGetMediaInfosResponse mediaInfosResponse = client.batchGetMediaInfosWithOptions(request, runtime);
            if (ObjectUtil.isNotNull(mediaInfosResponse)) {
                response = MediaManagerConvert.INSTANCE.convertGetMediaInfosRespVO(mediaInfosResponse.getBody());
            }
        } catch (TeaException error) {
            log.error(error.message);
        } catch (Exception e) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), e.getMessage());
        }
        return response;
    }
}
